{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "Python 3.7.6 64-bit ('pytorch': conda)",
   "display_name": "Python 3.7.6 64-bit ('pytorch': conda)",
   "metadata": {
    "interpreter": {
     "hash": "d4c1240a08c27a9e7d24f40f6f295dcde57dffc995969ffd0a60a858ccefdc00"
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 载入模型\n",
    "\n",
    "from ltp import LTP\n",
    "ltp = LTP() # 默认加载 Small 模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 分句\n",
    "from ltp import LTP\n",
    "ltp = LTP()\n",
    "sents = ltp.sent_split([\"他叫汤姆去拿外衣。汤姆生病了。他去了医院。\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "['他叫汤姆去拿外衣。', '汤姆生病了。', '他去了医院。']"
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "source": [
    "sents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用户自定义词典\n",
    "# user_dict.txt 是词典文件， max_window是最大前向分词窗口\n",
    "ltp.init_dict(path=\"user_dict.txt\", max_window=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[['他', '叫', '汤姆', '去', '拿', '外衣', '。']]"
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "# 分词\n",
    "segment, hidden = ltp.seg([\"他叫汤姆去拿外衣。\"])\n",
    "segment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "dict_keys(['word_cls', 'word_input', 'word_length', 'word_cls_input', 'word_cls_mask'])"
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "hidden.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[['r', 'v', 'nh', 'v', 'v', 'n', 'wp']]"
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "source": [
    "# 词性标注\n",
    "seg, hidden = ltp.seg([\"他叫汤姆去拿外衣。\"])\n",
    "pos = ltp.pos(hidden)\n",
    "pos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "('Nh', 2, 2)"
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "# 命名实体识别\n",
    "seg, hidden = ltp.seg([\"他叫汤姆去拿外衣。\"])\n",
    "ner = ltp.ner(hidden)\n",
    "ner[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Nh : 汤姆\n"
    }
   ],
   "source": [
    "tag, start, end = ner[0][0]\n",
    "print(tag,\":\", \"\".join(seg[0][start:end + 1]))\n",
    "# Nh : 汤姆"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[[[],\n  [('A0', 0, 0), ('A1', 2, 2), ('A2', 3, 5)],\n  [],\n  [],\n  [('A0', 2, 2), ('A1', 5, 5)],\n  [],\n  []]]"
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "# 语义角色标注\n",
    "seg, hidden = ltp.seg([\"他叫汤姆去拿外衣。\"])\n",
    "srl = ltp.srl(hidden)\n",
    "srl\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[[(1, [('A0', 0, 0), ('A1', 2, 2), ('A2', 3, 5)]),\n  (4, [('A0', 2, 2), ('A1', 5, 5)])]]"
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "source": [
    "srl = ltp.srl(hidden, keep_empty=False)\n",
    "srl"
   ]
  },
  {
   "source": [
    "### 依存句法分析"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在依存句法当中，虚节点ROOT占据了0位置，因此节点的下标从1开始\n",
    "dep = ltp.dep(hidden)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[[(1, 2, 'SBV'),\n  (2, 0, 'HED'),\n  (3, 2, 'DBL'),\n  (4, 5, 'ADV'),\n  (5, 2, 'VOB'),\n  (6, 5, 'VOB'),\n  (7, 2, 'WP')]]"
     },
     "metadata": {},
     "execution_count": 18
    }
   ],
   "source": [
    "dep"
   ]
  },
  {
   "source": [
    "`[['他', '叫', '汤姆', '去', '拿', '外衣', '。']]`\n",
    "\n",
    "| 得到的关系 | 名称| Description\n",
    "-|- | -\n",
    "(1, 2, 'SBV') | 动宾关系 | verb-object\n",
    "(2, 0, 'HED') | 核心关系 | head\n",
    "(3, 2, 'DBL') | 兼语 | double\n",
    "(4, 2, 'VOB') | 动宾关系 | verb-object\n",
    "(5, 4, 'COO') | 并列关系 | coordinate\n",
    "(6, 5, 'VOB') | 动宾关系 | \n",
    "(7, 2, 'WP')  | 标点符号 | punctuation\n"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}